import userService from '@/service/user.js';
import $store from '@/store/index.js';
import $go from './go.js';
import $cache from './cache.js';
import $push from './push.js';

module.exports = {

	checkLogin(user_token){
		let that = this;
		var token = user_token || $cache.token;
		console.log('checkLogin token',token);
		if (!token){
		// #ifdef MP-WEIXIN
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					let code = loginRes.code;
					//console.log('token................',code);
					if (code){
						let curRoute = $go.location(); 					
						that.getToken(code,curRoute);
					}
				},
			});
		
		// #endif
		// #ifdef H5
			console.log('App Check Token',token);
			let url = '/pages/user/login';
			let route = $go.location();	
			if (route && route.indexOf('pages/user/login')<0){
				url = url + (url.indexOf('?')>0?'&':'?')+'jumpUrl='+encodeURIComponent('/'+route)
			}
			uni.reLaunch({
				url: url,
			});
	
		// #endif
		
		// #ifdef APP-PLUS 
		let route = $go.location(); //获取当前页面栈
		if (!route || route.indexOf('pages/user/login')<0){
			let url = '/pages/user/login';
			if (route && route.indexOf('pages/user/login')<0){
				url = url + (url.indexOf('?')>0?'&':'?')+'jumpUrl='+encodeURIComponent('/'+route)
			}
			console.log('跳转到LOGIN',url);
			uni.reLaunch({
				url: url,
			});
			
		}
		// #endif
		}else{
			console.log('getUserInfo----');
			$store.dispatch('getUserInfo',true);
			$store.dispatch('getWallet',true);
			return true;
		}
		return false;
	},
	getToken(code,func,success_func) { //更新token/登陆
		if (typeof func==='string' && func.indexOf('friend.pay')>=0){
			console.log('func',func);
			return false;
		}
		userService.get(code).then((res) => {
			//console.log('red1313123', res)
			if (res.wechat_user && res.wechat_user.phone){
				let token=res.token;
				//console.log('tokentokentokentokentoken', res.token)
				$store.commit('login',{token});
				$store.dispatch('getUserInfo',true);
				$store.dispatch('getWallet',true);
				$push.getClientId(token);				
				setTimeout(function() {
					uni.$emit('user.login',token);
					clearTimeout();
				}, 200);
				if (success_func){
					console.log('success_func')
					success_func(res.token);
				}else{
					setTimeout(function() {
						uni.switchTab({
							url: '/pages/index/index'
						});
						clearTimeout();
					}, 200);					
				}
				
				
			}else{
				if (func){
					func(res.token);
				}else{
					uni.setStorageSync('login_token', res.token);
				}
				
			}
		}).catch(err => {
			
			
		})
	},

	
	quit() {
		let that = this;
		uni.showModal({
		    title: '退出确认',
		    content: '确认退出登录吗？',
		    success: function (res) {
		        if (res.confirm) {
		        	userService.logout().then((res) => {
						$store.commit('logout');
		        		uni.showToast({
		        			title: '退出成功！',
		        			icon: 'none',
		        			success() {
		          				uni.reLaunch({
		        					url: '/pages/user/login',
		        				});
		        			},
		        		})
		        	
		        	});
		        }
		    }
		});
		
	
	},
 
}
